package com.janke.softballnexus.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.janke.softballnexus.client.events.CreateDivision;
import com.janke.softballnexus.client.events.DeleteAllDivisions;
import com.janke.softballnexus.client.events.RetrieveAllDivisions;
import com.janke.softballnexus.client.events.UpdateDivision;
import com.janke.softballnexus.shared.DivisionProxy;
import com.janke.softballnexus.shared.SoftballRequestFactory;
import com.janke.softballnexus.shared.SoftballRequestFactory.DivisionRequest;

public class Mediator {

	final SoftballRequestFactory requests = GWT
			.create(SoftballRequestFactory.class);

	private static Mediator ref = null;

	private Mediator() {
		requests.initialize((SimpleEventBus) GWT.create(SimpleEventBus.class));

		DivisionEditorWorkflow.register(BagOfState.getInstance()
				.getClientEventBus(), requests);

		CreateDivision.register(BagOfState.getInstance().getClientEventBus(),
				new CreateDivision.Handler() {
					@Override
					public void onCreateDivision() {
						DivisionRequest createDivisionRequest = requests
								.divisionRequest();

						BagOfState
								.getInstance()
								.getClientEventBus()
								.fireEvent(
										new UpdateDivision(
												createDivisionRequest
														.create(DivisionProxy.class),
												createDivisionRequest));
					}
				});

		DeleteAllDivisions.register(BagOfState.getInstance()
				.getClientEventBus(), new DeleteAllDivisions.Handler() {

			@Override
			public void deleteAllDivisions(RequestContext requestContext) {
				DivisionRequest deleteAllDivisionsRequest = requests
						.divisionRequest();

				deleteAllDivisionsRequest.deleteAllDivions().fire(
						new Receiver<Void>() {

							@Override
							public void onSuccess(Void response) {
								Window.alert("all divisions deleted");
							}
						});

			}
		});

		RetrieveAllDivisions.register(BagOfState.getInstance()
				.getClientEventBus(), new RetrieveAllDivisions.Handler() {
			@Override
			public void onLoadDivisions() {
				DivisionRequest loadDivisionsRequest = requests
						.divisionRequest();
				loadDivisionsRequest.findAllDivions().fire(
						new Receiver<List<DivisionProxy>>() {
							@Override
							public void onSuccess(List<DivisionProxy> response) {
								BagOfState.getInstance().getAllDivisions()
										.clear();
								BagOfState.getInstance().getAllDivisions()
										.addAll(response);

								Window.alert(BagOfState.getInstance()
										.getAllDivisions().size()
										+ " divisions retrieved");
							}
						});
			}
		});

		DivisionEditorWorkflow.register(BagOfState.getInstance()
				.getClientEventBus(), requests);

	}

	public static Mediator getInstance() {
		if (ref == null) {
			ref = new Mediator();
		}
		return ref;
	}

	public void initialize() {
	}
}
